<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { DeletePatientDelAPI, GetPatientMylistAPI } from '@/apis/patient'
import { useConsultStore } from '@/stores/consult'
import type { GetPatientMylistParams } from '@/types/sub_form'
import { onReady, onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])
const patientList = ref<GetPatientMylistParams[]>([])
const getPatientMylist = async () => {
  const res = await GetPatientMylistAPI()
  // console.log(res)
  patientList.value = res.data
}
const onAdd = () => {
  uni.navigateTo({
    url: '/subpkg_archive/pages/list/list?type=consult',
  })
}
onShow(() => {
  getPatientMylist()
})
//删除患者信息
const onDelete = async (id: string, index: number) => {
  patientList.value.splice(index, 1)
  await DeletePatientDelAPI(id)
  store.patientId=''
  getPatientMylist()
}
const props = defineProps<{
  type?: 'consult'
}>()
onReady(() => {
  uni.setNavigationBarTitle({ title: props.type === 'consult' ? '选择患者' : '添加患者' })
})
//选择高亮
const store = useConsultStore()
const handleClick = (id: string) => {
  if(props.type!=='consult') return
  store.patientId = id
  console.log('患者id',store.patientId);
  
}
const onNext=()=>{
  uni.navigateTo({
    url: '/subpkg_consult/pages/payment/payment',
  })
}
</script>
<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="(item, index) in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onDelete(item.id, index)"
        >
          <view
            @click="handleClick(item.id)"
            class="archive-card"
            :class="{
              active:
                props.type === 'consult' ? item.id === store.patientId : item.defaultFlag === 1,
            }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '$1******$2')
              }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender === 1 ? '男' : '女' }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/pages/form/form?id=${item.id}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
              <uni-icons type="icon-edit" color="#16C2A3" size="24" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/pages/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label" @click="onAdd">添加患者</text>
        </navigator>
      </view>
      <!-- 下一步按钮，提交病人表单 -->
      <view class="next-step" v-if="props.type === 'consult'">
        <button :disabled="!store.patientId" class="uni-button" @click="onNext">下一步</button>
      </view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './list.scss';
</style>
